import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes.js'
import { getToken } from '@/utils/token.js'

Vue.use(VueRouter)

const a = VueRouter.prototype.push;

VueRouter.prototype.push =function push(location, onResolve, onReject) {

if (onResolve || onReject)return a.call(this, location, onResolve, onReject);

return a.call(this, location).catch(err => err);

}

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  let token = getToken()
  if(to.path !== '/login'){ // 不去登录页面去其他的页面
    console.log('去非登录的页面')
      if(token && token.token){ // 有token
        console.log('有token')
        next()
      } else { // 无 token
        console.log('没token')
        next('/login')
      }
  } else { // 去登录的页面
      if(token && token.token){
          console.log('有token了还去登录页面??')
          next('/layout')
      }else{
        console.log('去登录的页面')
          next()
      }
  }
})

export default router
